package com.wswy.wyjk.ui.drawable

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.support.v4.content.ContextCompat
import android.widget.TextView
import com.raft.framework.utils.UICompat
import com.wswy.wyjk.AppContext
import com.wswy.wyjk.R

/**
 * 题目类型
 */
class QuestionTypeDrawable(private val typeText:String) : BaseDrawable() {

    private val textPaint = TextView(AppContext.getContext()).apply {

        setTextColor(Color.WHITE)
        textSize = 12f

    }.paint

    init {

        val w = textPaint.measureText(typeText)

        val rectReal = Rect()

        rectReal.right = (w + UICompat.dp2px(10)).toInt()

        rectReal.bottom = UICompat.dp2px(20)


        bounds = rectReal

        paint.color = ContextCompat.getColor(AppContext.getContext(), R.color.colorPrimary_70)
        paint.style = Paint.Style.FILL
    }

    private val r = UICompat.dp2px(2).toFloat()

    override fun draw(canvas: Canvas) {

        canvas.drawRoundRect(rectF,r,r,paint)

        textPaint.color = Color.WHITE

        val x = rectF.width()/2f - textPaint.measureText(typeText)/2f
        val y = rectF.centerY() - textPaint.fontMetrics.top/2f - textPaint.fontMetrics.bottom/2f

        canvas.drawText(typeText,x,y,textPaint)

    }
}